@extends('admin.layouts.header_other')
@section('title',"后台管理系统")
@section('content')
    <table class="layui-hidden" id="treeTable" lay-filter="treeTable"></table>
    <script type="text/html" id="toolbarDemo">
        <div class="">
            <div class="layui-input-item">
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm padding_top" onclick="addrrr()">添加</button>
                </div>
            </div>

        </div>
    </script>
    <script type="text/html" id="switchTpl">
        <input type="checkbox" name="status" value="1" lay-skin="switch" lay-text="发布|未发布" lay-filter="statusChange" checked>
    </script>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
    <script src="{{asset('attach/js/layui.js')}}"></script>
    <script>
        var editObj=null,ptable=null,treeGrid=null,tableId='treeTable',layer=null;
        layui.config({
            base: '{{asset('attach/js/lay/extend/')}}'+'/'
        }).extend({
            treeGrid:'treeGrid'
        }).use(['jquery','treeGrid','layer'], function(){
            var $=layui.jquery;
            treeGrid = layui.treeGrid;//很重要
            layer=layui.layer;
            treeGrid.render({
                elem: '#treeTable'
                ,url:'{{route('admin.downloadcat.getlist')}}'
                ,cellMinWidth: 100
                ,idField:'id'//必須字段
                ,treeId:'id'//树形id字段名称
                ,treeUpId:'pid'//树形父id字段名称
                ,treeShowName:'title'//以树形式显示的字段
                ,heightRemove:[".dHead",10]//不计算的高度,表格设定的是固定高度，此项不生效
                ,height:'100%'
                ,toolbar: '#toolbarDemo'
                ,isFilter:false
                ,iconOpen:true//是否显示图标【默认显示】
                ,isOpenDefault:true//节点默认是展开还是折叠【默认展开】
                ,loading:true
                ,method:'get'
                ,isPage:false
                ,cols: [[
                    {type:'numbers'}
                    ,{type:'checkbox'}
                    ,{field:'title', width:300, title: '分类名称'}
                    ,{field:'psort',width:100, title: '排序'}
                    ,{field:'status', title: '是否启用',templet: '#switchTpl'}
                    ,{field:'description', title: '分类描述'}
                    ,{width:100,title: '操作', align:'center',toolbar: '#barDemo',}
                ]]
            });
            treeGrid.on('tool(treeTable)',function (obj) {
                switch(obj.event){
                    case 'edit':
                        var openwin=layer.open({
                            type:2,
                            title:"编辑菜单",
                            content:["{{route('admin.downloadcat.add')}}"],
                            area:['100%','100%'],
                            maxmin:true,
                            success: function(layero, index){
                                var body = layer.getChildFrame('body', index);
                                var iframeWin = window[layero.find('iframe')[0]['name']];
                                body.find('input[name=title]').val(obj.data.title);
                                body.find("input[name=status][value="+obj.data.status+"]").attr("checked",'checked');
                                body.find(".parent_id").find("option[value="+obj.data.parent_id+"]").attr("selected",true);
                                body.find('input[name=psort]').val(obj.data.psort);
                                body.find('input[name=menu_route]').val(obj.data.menu_route);
                                body.find('textarea[name=routes]').val(obj.data.routes);
                                body.find('textarea[name=description]').val(obj.data.description);
                            }
                        });
                        layer.full(openwin);
                        break;
                    case 'del':
                        var url="{{route('admin.downloadcat.add')}}",
                            data={id:obj.data.id,_token:$('meta[name="_token"]' ).attr('content')};
                        $.ajax({
                            type:'post',
                            url:url,
                            data:data,
                            dataType : "json",
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                            },
                            success:function (data) {
                                if(data.code==200){
                                    layer.msg(data.msg,{
                                        icon:6,
                                        time:2000
                                    },function () {
                                        location.reload();//刷新父页面
                                    });
                                }else{
                                    layer.msg(data.msg,{icon:5});
                                    return;
                                }
                            }
                        });
                        break;
                }
            });

        });
        function addrrr() {
            var openwin=layer.open({
                type:2,
                title:"添加下载分类",
                content:["{{route('admin.downloadcat.add')}}"],
                area:['100%','100%'],
                maxmin:true,
            });
            layer.full(openwin);
        }
    </script>
@endsection
